package com.example.areaofthesquare;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Utils {

    private List<Point> mPointList;
    private float mMaxDistance;
    private List<Point> mMaxPointList = new ArrayList<>();

    public void setmPointList(List<Point> mPointList) {
        this.mPointList = mPointList;
    }

    /**
     * 计算距离在最大的两个点的距离和索引
     *
     * @param pointList
     * @return
     */
    public List<Point> countMaxDistance(List<Point> pointList) {
        Log.i("countMaxDistance", "countMaxDistance: " + pointList.size());
        mMaxDistance = 0;
        for (int i = 0; i < pointList.size(); i++) {
            for (int j = i + 1; j < pointList.size(); j++) {
                float distance = countDistance(pointList.get(i), pointList.get(j));
                if (distance > mMaxDistance) {
                    mMaxDistance = distance;
                    if (mMaxPointList.size() < 2) {
                        mMaxPointList.add(pointList.get(i));
                        mMaxPointList.add(pointList.get(j));
                    } else if (mMaxPointList.size() == 2) {
                        mMaxPointList.set(0, pointList.get(i));
                        mMaxPointList.set(1, pointList.get(j));
                    }
                }

            }
        }
        return mMaxPointList;
    }

    /**
     * 计算两点之间的距离
     *
     * @param point1
     * @param point2
     * @return
     */
    public float countDistance(Point point1, Point point2) {
        float tempX = point1.getX() - point2.getX();
        float tempY = point1.getY() - point2.getY();
        return (float) Math.sqrt(tempX * tempX + tempY * tempY);
    }

    /**
     * 根据距离最远的两点的角度，旋转坐标系
     *
     * @param pointList
     * @return 返回一个旋转后的所有点的坐标的PointList
     */
    public List<Point> countSpinPointList(List<Point> pointList) {
        Log.i("startAndEndTime", "startTime: "+ getTime());
        mMaxPointList = countMaxDistance(pointList);
        //第一步，得出对角线与X轴的夹角
        Point point1 = mMaxPointList.get(0);
        Point point2 = mMaxPointList.get(1);
        float disToO;
        float cos;
        float angle;
        float sweepAngle;
        //第二步，求旋转角度
        if (Math.abs(point1.getX()) == (Math.abs(point1.getY()))) {
            if (Math.abs(point2.getX()) != Math.abs(point2.getY())) {
                disToO = countDistance(point1, point2);
                cos = Math.abs(point2.getX() - point1.getX()) / disToO;
                angle = (float) Math.acos(cos);
                sweepAngle = (float) (angle - Math.PI / 4);
            } else {
                return pointList;
            }
        } else {
            //计算两个点到原点的距离,得到45度对角线
            disToO = countDistance(point1, point2);
            Log.i("disToO", "disToO: " + disToO);
            cos = Math.abs(point2.getX() - point1.getX()) / disToO;
            Log.i("point1.getX()", "point1.getX(): " + point1.getX());
            Log.i("cos1", "cos: " + cos);
            angle = (float) Math.acos(cos);
            Log.i("angle", "angle: " + angle);
            sweepAngle = (float) Math.abs(angle - Math.PI / 4);
            Log.i("sweepAngle", "sweepAngle: " + sweepAngle);
        }
//        Log.i("mMaxPointList","point1"+point1.getX()+","+point1.getY());
//        Log.i("mMaxPointList","point2"+point2.getX()+","+point2.getY());
        //对角线与X轴的夹角的cos值
//        float cosMax = Math.abs(point1.getX() - point2.getX()) / mMaxDistance ;
        //使对角线旋转至45度位置

        //旋转其余点
        for (int i = 0; i < pointList.size(); i++) {
            float x = (float) (pointList.get(i).getX() * Math.cos(sweepAngle) - pointList.get(i).getY() * Math.sin(sweepAngle));
            float y = (float) (pointList.get(i).getY() * Math.cos(sweepAngle) + pointList.get(i).getX() * Math.sin(sweepAngle));
            if (pointList.get(i).getX() == mMaxPointList.get(0).getX() && pointList.get(i).getY() == mMaxPointList.get(0).getY()) {
                mMaxPointList.set(0, new Point(x, y));
            }
            if (pointList.get(i).getX() == mMaxPointList.get(1).getX() && pointList.get(i).getY() == mMaxPointList.get(1).getY()) {
                mMaxPointList.set(1, new Point(x, y));
            }
            pointList.get(i).setX(x);
            pointList.get(i).setY(y);
        }
        return pointList;
//        point1.setX((float) (dis1ToO * Math.cos(45)));
//        point1.setY(point1.getX());
//        point2.setX((float) (mMaxDistance * Math.cos(45) + point1.getX()));
//        point2.setY(point2.getX());
    }

    public List<Point> getmMaxPointList() {
        return mMaxPointList;
    }

    /**
     * float类型保留两位小数点
     *
     * @param num
     * @return
     */
    public static float decimalSize(float num) {
        return (float) (Math.round(num * 1000)) / 1000;
    }

    public List<Point> countNewRectF(List<Point> pointList) {
        List<Point> newSquareList = new ArrayList<>();
        //根据距离最大的两点可以得到一组极值
        //判断完高低左右之后，遍历所有的点，看是否满足在区域内
        //如果不满足条件的，根据flag判断出具体不满足哪个条件
        //即判断是左上还是右下，分别将最长的线段的两端的点往对应方向延申差值，得到新的正方形点list，遍历结束后，返回这个list

        //1.得极值
        float leftSide = 0;
        float rightSide = 0;
        float upSide = 0;
        float downSide = 0;

        if (mMaxPointList.get(0).getX() < mMaxPointList.get(1).getX()) {
            leftSide = mMaxPointList.get(0).getX();
            rightSide = mMaxPointList.get(1).getX();
        } else {
            rightSide = mMaxPointList.get(0).getX();
            leftSide = mMaxPointList.get(1).getX();
        }
        if (mMaxPointList.get(0).getY() < mMaxPointList.get(1).getY()) {
            downSide = mMaxPointList.get(0).getY();
            upSide = mMaxPointList.get(1).getY();
        } else {
            upSide = mMaxPointList.get(0).getY();
            downSide = mMaxPointList.get(1).getY();
        }
        //依次按照左下，左上，右上，右下的顺序构建新的List
        newSquareList.add(new Point(leftSide, downSide));
        newSquareList.add(new Point(leftSide, upSide));
        newSquareList.add(new Point(rightSide, upSide));
        newSquareList.add(new Point(rightSide, downSide));
        String flag = "";
        //判断两点是否是左上点或右下
        if (equalPoint(newSquareList.get(0),mMaxPointList.get(0)) || equalPoint(newSquareList.get(3),mMaxPointList.get(0))) {
            flag = "right";
        }else {
            flag = "left";
        }
        //2. 遍历点，检索区域
        for (int i = 0; i < pointList.size(); i++) {
            float pointX = pointList.get(i).getX();
            float pointY = pointList.get(i).getY();
            float countLeft = pointX - leftSide;
            float countRight = pointX - rightSide;
            float countUp = pointY - upSide;
            float countDown = pointY - downSide;
            //是否左边
            if (countLeft < 0) {
                //是否左上或左下
                if (countUp > 0) {
                    //左上 ,对角线也是左上的,偏移量的变化量取大
                    float maxChange = Math.max(Math.abs(countLeft), Math.abs(countUp));
//                    float minChange = Math.min(Math.abs(countLeft), Math.abs(countUp));
                    if (flag.equals("left")){
                            //第一个点是左上
                            //偏左或偏上
                            leftSide = leftSide - maxChange;
                            upSide = upSide + maxChange;
                    } else{
                        //左上，对角线右上的，左下的偏移量的变化量取小，右上的取大
                        leftSide = leftSide - Math.abs(countLeft);
                        downSide = downSide - Math.abs(countLeft);
                        rightSide = rightSide + Math.abs(countUp);
                        upSide = upSide + Math.abs(countUp);
                    }
                } else if (countDown < 0) {
                    //左下
                    float maxChange = Math.max(Math.abs(countLeft), Math.abs(countDown));
                    if (flag.equals("left")){
                        leftSide = leftSide - Math.abs(countLeft);
                        upSide = upSide + Math.abs(countLeft);
                        rightSide = rightSide + Math.abs(countDown);
                        downSide = downSide -  Math.abs(countDown);
                    } else {
                        leftSide = leftSide - maxChange;
                        downSide = downSide - maxChange;
                    }
                } else {
                    //左中
                    if (flag.equals("left")){
                        Log.i("left", "leftCenterleft");
                        leftSide = leftSide - Math.abs(countLeft);
                        upSide = upSide + Math.abs(countLeft);
                    } else {
                        Log.i("left", "leftCenterright");
                        leftSide = leftSide - Math.abs(countLeft);
                        downSide = downSide - Math.abs(countLeft);
                    }
                }
            } else {
                //是否右边
                float maxChange = Math.max(Math.abs(countRight), Math.abs(countUp));
                if (countRight > 0) {
                    //是否右上或右下
                    if (countUp > 0) {
                        //右上
                        if (flag.equals("left")){
                            rightSide = rightSide + Math.abs(countRight);
                            downSide = downSide - Math.abs(countRight);
                            leftSide = leftSide - Math.abs(countUp);
                            upSide = upSide + Math.abs(countUp);
                        } else{
                            rightSide = rightSide + maxChange;
                            upSide = upSide + maxChange;
                        }
                    } else if (countDown < 0) {
                        //右下
                        if (flag.equals("left")){
                            rightSide = rightSide + maxChange;
                            downSide = downSide - maxChange;
                        } else{
                            rightSide = rightSide + Math.abs(countRight);
                            upSide = upSide + Math.abs(countRight);
                            downSide = downSide - Math.abs(countDown);
                            leftSide = leftSide - Math.abs(countDown);
                        }
                    } else {
                        //右中
                        if (flag.equals("left")){
                            rightSide = rightSide + Math.abs(countRight);
                            downSide = downSide - Math.abs(countRight);
                        } else {
                            rightSide = rightSide + Math.abs(countRight);
                            upSide = upSide + Math.abs(countRight);
                        }
                    }
                }
            }
            //是否上下(仅仅上下而已)
            if (countUp > 0 && countLeft > 0 && countRight < 0) {
                //上方
                if (flag.equals("left")){
                    leftSide = leftSide - Math.abs(countUp);
                } else {
                    rightSide = rightSide + Math.abs(countUp);
                }
                upSide = upSide + Math.abs(countUp);
            }else if(countDown < 0 && countLeft > 0 && countRight < 0) {
                //下方
                if (flag.equals("left")){
                    rightSide = rightSide + Math.abs(countDown);
                } else {
                    leftSide = leftSide - Math.abs(countDown);
                }
                downSide = downSide - Math.abs(countDown);
            }
                    newSquareList.get(0).setX(leftSide);
                    newSquareList.get(0).setY(downSide);
                    newSquareList.get(1).setX(leftSide);
                    newSquareList.get(1).setY(upSide);
                    newSquareList.get(2).setX(rightSide);
                    newSquareList.get(2).setY(upSide);
                    newSquareList.get(3).setX(rightSide);
                    newSquareList.get(3).setY(downSide);
                    if (i == pointList.size() -1) {
//                        Log.i("resultSize", "resultSize: "+rightSide);
//                        Log.i("resultSize", "resultSize: "+leftSide);
//                        Log.i("resultSize", "resultSize: "+upSide);
//                        Log.i("resultSize", "resultSize: "+downSide);
                        double size = (rightSide - leftSide) * (upSide - downSide);
                        Log.i("startAndEndTime", "resultTime: "+ getTime());
//                        Log.i("resultSize", "resultSize: "+size);
                    }
        }
        return newSquareList;
    }

    public boolean equalPoint(Point p1,Point p2){
        if (p1.getX() == p2.getX() && p1.getY() == p2.getY()){
            return true;
        }
        else return false;
    }



    public static String getTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());
        return date;
    }






















}
